package com.xw.childhealth.controller;

import com.xw.childhealth.VO.DataDictionaryVO;
import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.JieduanKnowledge;
import com.xw.childhealth.service.JieduanKnowledgeService;
import com.xw.childhealth.util.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-13 11:32
 * @Version: 1.0
 */
@RestController
@RequestMapping("/jieduanKnowledge")
@Slf4j
public class JieduanKnowledgeController {


    @Autowired
    private JieduanKnowledgeService jieduanKnowledgeService;


    /**
     * 查询分阶段发育与养育知识库
     *
     * @return
     */
    @PostMapping("/getJieduanKnowledge")
    public ResultVO getJieduanKnowledge() {

        List<DataDictionaryVO> list = new ArrayList<>();
        List<DataDictionaryVO> result = recursionData(list, 0);

        return ResultVOUtil.success(result);
    }


    private List<DataDictionaryVO> recursionData(List<DataDictionaryVO> jieduanKnowledgeList, Integer id) {

        List<JieduanKnowledge> parentNode = jieduanKnowledgeService.findByParentIdEquals(id);
        if (parentNode == null || parentNode.isEmpty()) {
            return jieduanKnowledgeList;
        }
        parentNode.forEach(e -> {
            DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
            dataDictionaryVO.setTitle(e.getInformation());
            dataDictionaryVO.setId(e.getId());
            if (e.getInformation() != null) {
                log.info("");
                List<DataDictionaryVO> healthGuidances = recursionData(new ArrayList<>(), e.getId());
                dataDictionaryVO.setChildren(healthGuidances.size() == 0 ? null : healthGuidances);
                jieduanKnowledgeList.add(dataDictionaryVO);
            }

        });

        return jieduanKnowledgeList;
    }

    /**
     * 查找节点下面的内容
     *
     * @param nodeId
     * @return
     */
    @PostMapping("/getNodeInfo/{nodeId}")
    public ResultVO getNodeInfo(@PathVariable Integer nodeId) {
        List<JieduanKnowledge> result = jieduanKnowledgeService.findByParentIdEquals(nodeId);
        return ResultVOUtil.success(result);
    }


    /**
     * 添加更新
     *
     * @param type
     * @param nodeId
     * @param value1
     * @param value2
     * @param category
     * @param addRecordNode
     * @param map
     * @return
     */
    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type, @RequestParam(value = "nodeId", required = false) Integer nodeId,
                             @RequestParam(value = "value1", required = false) String value1, @RequestParam(value = "value2", required = false) String value2,
                             @RequestParam(value = "category", required = false) String category,
                             @RequestParam(value = "addRecordNode", required = false) Integer addRecordNode, Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/jieduan-knowledge", map);
        }
        if (type.equals("toUpd")) {
            JieduanKnowledge one = jieduanKnowledgeService.findByIdEquals(nodeId);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            JieduanKnowledge one = jieduanKnowledgeService.findByIdEquals(nodeId);
            one.setDetail(value2);
            jieduanKnowledgeService.save(one);
            return new ModelAndView("/pages/admin/jieduan-knowledge", map);
        }
        if (type.equals("toAdd")) {
            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            List<JieduanKnowledge> list1 = jieduanKnowledgeService.findByInformationListEquals(value1);
            if (list1.size() == 0) {
                JieduanKnowledge result1 = jieduanKnowledgeService.save(new JieduanKnowledge().setCreateTime(new Date()).setUpdateTime(new Date()).setInformation(value1).setParentId(0));
                if (result1 != null) {
                    JieduanKnowledge save = jieduanKnowledgeService.save(new JieduanKnowledge().setCreateTime(new Date()).setUpdateTime(new Date()).setParentId(result1.getId()).setInformation(category));
                    if (save != null) {
                        jieduanKnowledgeService.save(new JieduanKnowledge().setCreateTime(new Date()).setUpdateTime(new Date()).setParentId(save.getId()).setDetail(value2));
                    }
                }
            } else {
                List<JieduanKnowledge> res = jieduanKnowledgeService.findByParentIdEquals(list1.get(0).getId());

                for (JieduanKnowledge e : res) {
                    if (e.getInformation() != null) {
                        if (e.getInformation().equals(category)) {
                            return new ModelAndView("/pages/admin/jieduan-knowledge");
                        }
                    }
                }
            }

            if (list1.size() != 0) {
                JieduanKnowledge save = jieduanKnowledgeService.save(new JieduanKnowledge().setCreateTime(new Date()).setUpdateTime(new Date()).setParentId(list1.get(0).getId()).setInformation(category));
                if (save != null) {
                    jieduanKnowledgeService.save(new JieduanKnowledge().setCreateTime(new Date()).setUpdateTime(new Date()).setParentId(save.getId()).setDetail(value2));
                }
            }
            return new ModelAndView("/pages/admin/jieduan-knowledge", map);
        }
        return new ModelAndView("/pages/admin/jieduan-knowledge-data-add", map);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        JieduanKnowledge result = jieduanKnowledgeService.findByIdEquals(id);
        if (result != null) {
            JieduanKnowledge result2 = jieduanKnowledgeService.findByIdEquals(result.getParentId());
            jieduanKnowledgeService.delRecord(id);
            jieduanKnowledgeService.delRecord(result2.getId());
        }
        return new ModelAndView("/pages/admin/jieduan-knowledge");
    }
}
